<template>
  <inline-Modal
    :visible="visible"
    title="上月交办事项完成情况"
    :showOk="false"
    @cancel="handelCancel()"
  >
    <a-button
      type="primary"
      class="add-btn"
      :loading="loading"
      @click="handelOk()"
      v-if="operateType == 2"
    >
      确定
    </a-button>
    <div class="lastMonth-page-container">
      <a-form-model
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :hideRequiredMark="isDisabled"
        colon
      >
        <div class="base-container">
          <a-form-model-item label="例会月份">
            <a-month-picker
              style="width:33%"
              format="YYYY-MM"
              valueFormat="YYYY-MM"
              v-model="form.monthData"
              disabled
            />
          </a-form-model-item>
          <div
            class="card-item"
            v-for="(item, index) in form.childData"
            :key="index"
          >
            <div class="card-header">交办事项{{ index + 1 }}</div>
            <div class="card-body">
              <a-row :gutter="50">
                <a-col :span="12">
                  <a-form-model-item label="交办单位">
                    <!-- <a-select
                      mode="multiple"
                      v-model="item.assigningUnits"
                      disabled
                    >
                      <a-select-option
                        :value="v.dicItemCode"
                        v-for="v in dictSoucre"
                        :key="v.uuid"
                      >
                        {{ v.dicItemName }}
                      </a-select-option>
                    </a-select> -->
                    <a-tree-select
                      multiple
                      disabled
                      v-model="item.assigningUnits"
                      style="width: 100%"
                      :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                      :replaceFields="{
                        children: 'children',
                        title: 'orgName',
                        key: 'id',
                        value: 'uuid',
                      }"
                      :tree-data="treeData"
                    >
                    </a-tree-select>
                  </a-form-model-item>
                </a-col>
                <a-col :span="12">
                  <a-form-model-item label="交办完成单位">
                    <!-- <a-select
                      mode="multiple"
                      v-model="item.assigningCompleteUnits"
                      disabled
                    >
                      <a-select-option
                        :value="v.dicItemCode"
                        v-for="v in dictSoucre"
                        :key="v.uuid"
                      >
                        {{ v.dicItemName }}
                      </a-select-option>
                    </a-select> -->
                    <a-tree-select
                      multiple
                      disabled
                      v-model="item.assigningCompleteUnits"
                      style="width: 100%"
                      :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                      :replaceFields="{
                        children: 'children',
                        title: 'orgName',
                        key: 'id',
                        value: 'uuid',
                      }"
                      :tree-data="treeData"
                    >
                    </a-tree-select>
                  </a-form-model-item>
                </a-col>
              </a-row>
              <a-form-model-item label="配合单位">
                <!-- <a-select
                  mode="multiple"
                  v-model="item.cooperateUnits"
                  disabled
                >
                  <a-select-option
                    :value="v.dicItemCode"
                    v-for="v in dictSoucre"
                    :key="v.uuid"
                  >
                    {{ v.dicItemName }}
                  </a-select-option>
                </a-select> -->
                <a-tree-select
                  disabled
                  v-model="item.cooperateUnits"
                  style="width: 100%"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  multiple
                  :replaceFields="{
                    children: 'children',
                    title: 'orgName',
                    key: 'id',
                    value: 'uuid',
                  }"
                  :tree-data="treeData"
                >
                </a-tree-select>
              </a-form-model-item>
              <a-form-model-item label="交办事项">
                <a-input
                  type="textarea"
                  :rows="3"
                  v-model="item.tasks"
                  disabled
                ></a-input>
              </a-form-model-item>
              <a-form-model-item
                label="完成情况"
                :prop="'childData.' + index + '.completionStatus'"
                :rules="{
                  required: true,
                  message: '请输入完成情况',
                }"
              >
                <a-input
                  placeholder="请输入"
                  type="textarea"
                  :rows="4"
                  v-model="item.completionStatus"
                  :disabled="isDisabled"
                ></a-input>
              </a-form-model-item>
            </div>
          </div>
        </div>
      </a-form-model>
    </div>
  </inline-Modal>
</template>

<script>
import { getDicItemByCode } from "@/api/common.js";
import { getTBRTaskDetailByMonth, saveTBRTaskData } from "@/api/safetyManage";
export default {
  props: {
    visible: {
      type: Boolean,
      required: false,
    },
    parameter: {
      type: Object,
      default: () => {
        return {};
      },
    },
    operateType: {
      type: [String, Number],
      default: "",
    },
  },
  data() {
    return {
      loading: false,
      form: {
        //月份
        monthData: "",
        //上月交办事项
        childData: [],
      },
      rules: {},
      //字典集合
      dictSoucre: [],
      treeData: [],
    };
  },
  computed: {
    isDisabled() {
      if (this.operateType == 1) {
        return true;
      } else {
        return false;
      }
    },
  },
  watch: {
    visible: {
      handler(val) {
        this.$emit("update:visible", val);
        if (val) {
          Promise.all([this.getTreeSelectData(), this.getDetail()]);
        }
      },
      immediate: true,
    },
  },
  mounted() {},
  methods: {
    //查询交办单位
    getTreeSelectData() {
      return new Promise((resolve, reject) => {
        this.$post("/mdata/organization/getTreeSysOrganizationByOrg", {})
          .then((res) => {
            let { data } = res;
            this.treeData = data;
            resolve();
          })
          .catch((_) => {
            this.treeData = [];
            reject();
          })
          .finally((_) => {});
      });
    },
    //查询字典表
    getDicCodeData() {
      return new Promise((resolve, reject) => {
        getDicItemByCode({ code: "affiliatedUnit" })
          .then((res) => {
            let { data } = res;
            this.dictSoucre = data;
            resolve();
          })
          .catch((_) => {
            this.dictSoucre = [];
            reject();
          })
          .finally((_) => {});
      });
    },
    //详情
    getDetail() {
      let { monthData } = this.parameter;
      return new Promise((resolve, reject) => {
        getTBRTaskDetailByMonth({ monthData })
          .then((res) => {
            let { data } = res;
            this.form.monthData = monthData;
            this.form.childData = [];
            (data ?? []).forEach((v) => {
              let {
                assigningUnits,
                assigningCompleteUnits,
                cooperateUnits,
                tasks,
                completionStatus,
                uuid,
              } = v;
              this.form.childData.push({
                assigningUnits: assigningUnits ? assigningUnits.split(",") : [],
                assigningCompleteUnits: assigningCompleteUnits
                  ? assigningCompleteUnits.split(",")
                  : [],
                cooperateUnits: cooperateUnits ? cooperateUnits.split(",") : [],
                tasks,
                completionStatus,
                uuid,
              });
            });

            resolve();
          })
          .catch((_) => {
            reject();
          });
      });
    },
    handelOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          let { form } = this;
          let formJson = [];
          form.childData.forEach((v) => {
            formJson.push({
              uuid: v.uuid,
              completionStatus: v.completionStatus,
            });
          });
          saveTBRTaskData(formJson)
            .then((res) => {
              if (res.statusCode === 200 || res === 1) {
                this.$message.success("操作成功", 1.5);
                this.handelCancel();
              } else {
                this.$message.error(res?.message ?? "操作失败");
              }
            })
            .catch((_) => {})
            .finally((_) => {
              this.loading = false;
            });
        }
      });
    },
    handelCancel() {
      this.$parent.getTableData();
      this.$emit("update:visible", false);
    },
  },
};
</script>

<style lang="less" scoped>
.add-btn {
  position: absolute;
  top: 10px;
  right: 90px;
}
.lastMonth-page-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: auto;
  padding: 10px 20px 20px 20px;
  /deep/.ant-form-item {
    display: flex;
    .ant-form-item-label {
      flex-shrink: 0;
      width: 70px;
      white-space: pre-wrap;
      display: flex;
      justify-content: flex-end;
      line-height: 20px;
      margin-right: 2px;
      align-items: center;
      height: 36px;
    }
    .ant-form-item-control-wrapper {
      flex: 1;
    }
  }
  .base-container {
    padding: 0 60px 0 0;
    .card-item {
      border-radius: 6px 6px 6px 6px;
      box-shadow: 0px 0px 3px 2px rgba(51, 51, 51, 0.14);
      margin: 16px 0;
      .card-header {
        padding: 16px 24px;
        box-sizing: border-box;
        font-family: Microsoft YaHei, Microsoft YaHei;
        font-weight: 400;
        font-size: 16px;
        color: #666666;
        letter-spacing: 1px;
        border-bottom: 1px solid #d9d9d9;
        position: relative;
        .delete-icon {
          cursor: pointer;
          width: 40px;
          height: 32px;
          border-radius: 0px 6px 0px 50px;
          background: rgba(255, 45, 45, 0.2);
          position: absolute;
          top: 0;
          right: 0;

          /deep/.anticon-close {
            position: absolute;
            top: 50%;
            left: 60%;
            transform: translate(-50%, -50%);
            color: #ff2d2d;
          }
        }
      }
      .card-body {
        padding: 10px 20px;
        box-sizing: border-box;
        /deep/.ant-form-item {
          .ant-form-item-label {
            width: 68px;
          }
        }
        /deep/.ant-select-disabled
          .ant-select-selection--multiple
          .ant-select-selection__choice {
          background-color: #fafafa !important;
          color: #000000 !important;
        }
      }
      &:last-of-type {
        margin-bottom: 0;
      }
    }
  }
}
</style>
